﻿if (typeof(Admin) == 'undefined')
    Admin = {};
if (typeof (Admin.Product) == 'undefined')
    Admin.Product = {};

Admin.Product.List = {
    _groupProduct: function () { return $("#groupProduct").val() },
    _brand: function () { return $("#brand").val() },
    _pageMax: function () { return $("#pageMax").val() },
    _pageIndex: function () { return $("#pageIndex").val() },
    _options: {
        id: 'gridList',
        url: function () {
            var ret = "Service/ListProduct.ashx?key=e"

            var gp = Admin.Product.List._groupProduct();
            var bi = Admin.Product.List._brand();
            var pm = Admin.Product.List._pageMax();
            var pi = Admin.Product.List._pageIndex();

            if (gp.length > 0)
                ret += "&gi=" + gp;
            if (bi.length > 0)
                ret += "&bi=" + bi;
            if (pm.length > 0)
                ret += "&pm=" + pm;
            if (pi.length > 0)
                ret += "&pi=" + pi;
            return ret;
        }
    },
    GetDataJson: function (url) {
        return new TIO().ajax(url);
    },
    ManipulateData: function (options) {
        var obj = $("#" + options.id + " > tbody");
        var data = new TIO().ajax(options.url());
        var result = "";
        for (i = 0; i < data.length; i++) {
            var item = data[i];
            result += $.Format(_formatProduct, item.ProductId, item.ProductName, item.ProductPrice, item.GroupProductName, item.BrandName);
        }
        obj.html(result);
    },
    CreateGroupProduct: function () {
        var obj = $("#groupProduct");
        var data = new TIO().ajax("Service/ListGroupProduct.ashx");
        var result = "<option selected=\"selected\" label=\"All\" value=\"0\">General</option>";
        for (i = 0; i < data.length; i++) {
            var item = data[i];
            result += $.Format(_formatGroupProduct, item.GroupProductName, item.GroupProductId);
        }
        obj.html(result);
    },
    CreateBrand: function () {
        var obj = $("#brand");
        var data = new TIO().ajax("Service/ListBrand.ashx");
        var result = "<option selected=\"selected\" label=\"All\" value=\"0\">General</option>";
        for (i = 0; i < data.length; i++) {
            var item = data[i];
            result += $.Format(_formatBrands, item.BrandName, item.BrandId);
        }
        obj.html(result);
    },
    UpdatePhotosLink: function () {
        var link1 = $("#upload1").attr("filename");
        var link2 = $("#upload2").attr("filename");
        var link3 = $("#upload3").attr("filename");
        var link4 = $("#upload4").attr("filename");
        var link5 = $("#upload5").attr("filename");
        $("input[id$='photos1']").val(link1);
        $("input[id$='photos2']").val(link2);
        $("input[id$='photos3']").val(link3);
        $("input[id$='photos4']").val(link4);
        $("input[id$='photos5']").val(link5);
    },
    Edit: function (pid) {
        var height = window.screen.height - 130;
        window.open("ProductEdit.aspx?pi=" + pid, "_blank", "resizable=1,width=785,height=" + height);
    },
    Delete: function (pid) {
        var data = new TIO().ajax("Service/DeleteProduct.ashx?pid=" + pid);
        this.ManipulateData(this._options);
    },
    InitEvent: function () {
        var thisObj = this;
        $("#pageIndex").keyup(function (e) {
            var key = e.charCode || e.keyCode || 0;
            if (key == 48 || key == 49 || key == 50 || key == 51 || key == 52 || key == 53 ||
                key == 54 || key == 55 || key == 56 || key == 57) {
                thisObj.ManipulateData(thisObj._options);
            }
        });
        $("#pageMax").change(function () {
            thisObj.ManipulateData(thisObj._options);
        });
        $("#btnFilter").click(function () {
            thisObj.ManipulateData(thisObj._options);
            return false;
        });
    },
    InitUploader: function () {
        var setting = { action: 'Service/UploadImageProduct.ashx' };
        Admin.Core.Uploader('upload1', setting);
        Admin.Core.Uploader('upload2', setting);
        Admin.Core.Uploader('upload3', setting);
        Admin.Core.Uploader('upload4', setting);
        Admin.Core.Uploader('upload5', setting);
    },
    Init: function () {
        this.InitEvent();
        this.InitUploader();
        this.CreateGroupProduct();
        this.CreateBrand();
        this.ManipulateData(this._options);
    }
}

$(function () {
    Admin.Product.List.Init();
});

_formatBrands = "<option label=\"{0}\" value=\"{1}\">{1}</option>";
_formatGroupProduct = "<option label=\"{0}\" value=\"{1}\">{1}</option>";

_formatProduct = "<tr>" + 
"<td class='a-center'>{0}</td>" +
"<td><a href='#'>{1}</a></td>" +
"<td>{2}</td>" +
"<td>{3}</td>" +
"<td>{4}</td>" +
"<td>" +
    "<a pid='{0}' href='#'><img src='../../img/icons/user.png' title='Show detail' width='16' height='16' /></a>" +
    "<a pid='{0}' href='javascript:Admin.Product.List.Edit({0});'><img src='../../img/icons/user_edit.png' title='Edit product' width='16' height='16' /></a>" +
    "<a pid='{0}' href='javascript:Admin.Product.List.Delete({0});'><img src='../../img/icons/user_delete.png' title='Delete product' width='16' height='16' /></a>" +
"</td>" +
"</tr>";